package views;

import java.util.Random;

import model.EnhancedCostEvaluator;
import model.IncrementalCostCalculator;
import model.IncrementalValidator;
import model.MachineSelector;
import model.SolverEnvironment;
import model.Problem;
import model.Solution;
import solver.CGHillClimber;
import solver.LateAcceptanceHeuristicSolver;
import solver.moves.AbstractMove;
import util.Chronograph;
import util.Importer;
import util.SolutionImporter;

import command.Invoker;

public class TestPACode {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Chronograph chronos = new Chronograph();
		chronos.reset();
		TestPACode app = new TestPACode();
//		app.launch1();
		app.launch2();
//		app.launch3();
		chronos.lap();
		System.out.println("Time elapsed:" + chronos.getElapsedTimeInSeconds());
	}

	public void launch1() {
		String dataset = "datasets//model_a1_2.txt";
		String initial_solution_s = "solutions//assignment_a1_2.txt";
		Importer importer = new Importer(dataset);
		importer.readData();
		Problem problem = importer.getProblem();
		SolutionImporter solutionImporter = new SolutionImporter(problem);
		solutionImporter.setSolutionFileName(initial_solution_s);
		Solution initial_solution = solutionImporter.readData();
		Solution current_solution = initial_solution.copy();
		Invoker invoker = new Invoker();
		IncrementalCostCalculator cc = new IncrementalCostCalculator();
		IncrementalValidator iv = new IncrementalValidator();
		EnhancedCostEvaluator ece = new EnhancedCostEvaluator();
		SolverEnvironment env = new SolverEnvironment(problem, initial_solution,
				current_solution, new Random(), invoker, iv, cc,ece);
		MachineSelector ms = new MachineSelector(env);
		for (int i = 0; i < 10; i++) {
			int x = ms.selectLeastLoadedMachine(10);
			int y = ms.selectMostLoadedMachine(10);
			System.out.println(x + " " + y);
			
		}
	}
	
	public void launch2(){
		int TIME_LIMIT = 20;
		String dataset = "datasets//model_a1_2.txt";
		String initial_solution_s = "solutions//assignment_a1_2.txt";
		Importer importer = new Importer(dataset);
		importer.readData();
		Problem problem = importer.getProblem();
		SolutionImporter solutionImporter = new SolutionImporter(problem);
		solutionImporter.setSolutionFileName(initial_solution_s);
		Solution solution1 = solutionImporter.readData();
		LateAcceptanceHeuristicSolver solver = new LateAcceptanceHeuristicSolver(
				problem, solution1);
		int usedTime = solver.solveWithTimeLimit(TIME_LIMIT);
		EnhancedCostEvaluator ece = new EnhancedCostEvaluator();
		Solution new_solution = solver.getNewSolution();
		ece.setSolutionPair(solution1, new_solution);
		ece.computeCost();
		boolean f = ece.isFeasible();
		String sol_filename = initial_solution_s.replaceAll("\\.txt",
				"");
		String fn = String.format("%s_%s%s[%s].txt", sol_filename,
				ece.getNewSolutionCost(), f ? "" : "_inf", TIME_LIMIT);
		new_solution.exportToFileName(fn);
		System.out.println("File " + fn + " saved!");
	}
	
	public void launch3(){
		int TIME_LIMIT = 20;
		String dataset = "datasets//model_a1_2.txt";
		String initial_solution_s = "solutions//assignment_a1_2.txt";
		Importer importer = new Importer(dataset);
		importer.readData();
		Problem problem = importer.getProblem();
		SolutionImporter solutionImporter = new SolutionImporter(problem);
		solutionImporter.setSolutionFileName(initial_solution_s);
		Solution solution1 = solutionImporter.readData();
		CGHillClimber solver = new CGHillClimber(
				problem, solution1);
		int usedTime = solver.solveWithTimeLimit(TIME_LIMIT);
		EnhancedCostEvaluator ece = new EnhancedCostEvaluator();
		Solution new_solution = solver.getNewSolution();
		ece.setSolutionPair(solution1, new_solution);
		ece.computeCost();
		boolean f = ece.isFeasible();
		String sol_filename = initial_solution_s.replaceAll("\\.txt",
				"");
		String fn = String.format("%s_%s%s[%s].txt", sol_filename,
				ece.getNewSolutionCost(), f ? "" : "_inf", TIME_LIMIT);
		new_solution.exportToFileName(fn);
		System.out.println("File " + fn + " saved!");
	}

	
}
